package com.ljl.ssm.service.impl;

import com.ljl.ssm.entity.Order;
import com.ljl.ssm.entity.OrderItem;
import com.ljl.ssm.entity.Product;
import com.ljl.ssm.mapper.OrderItemMapper;
import com.ljl.ssm.pojo.OrderItemExample;
import com.ljl.ssm.service.IOrderItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljl.ssm.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author BaoZhou
 * @since 2019-07-07
 */
@Service
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements IOrderItemService {

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    IProductService productService;

    @Override
    public OrderItem getById(Integer id) {
        OrderItem orderItem = orderItemMapper.selectByPrimaryKey(id);
        setProduct(orderItem);
        return orderItem;
    }

    @Override
    public void add(OrderItem orderItem) {
        orderItemMapper.insert(orderItem);
    }

    @Override
    public List<OrderItem> getByOrderId(Integer user_id) {
        OrderItemExample orderExample=new OrderItemExample();
        orderExample.or().andUser_idEqualTo(user_id);
        List<OrderItem> result = orderItemMapper.selectByExample(orderExample);
        setProduct(result);
        return result;
    }

    @Override
    public void update(OrderItem orderItem) {
        orderItemMapper.updateByPrimaryKey(orderItem);
    }

    @Override
    public List<OrderItem> listByUserId(Integer user_id) {
        OrderItemExample example = new OrderItemExample();
        example.or().andUser_idEqualTo(user_id);
        List<OrderItem> result = orderItemMapper.selectByExample(example);
        setProduct(result);
        return result;
    }

    @Override
    public List<OrderItem> listForCart(Integer user_id) {
        OrderItemExample example = new OrderItemExample();
        example.or().andUser_idEqualTo(user_id).andOrder_idIsNull();
        List<OrderItem> result = orderItemMapper.selectByExample(example);
        setProduct(result);
        return result;
    }

    @Override
    public void delete(Integer id) {
        orderItemMapper.deleteById(id);
    }

    @Override
    public void fill(List<Order> orders) {
        for (Order o : orders) {
            fill(o);
        }
    }

    @Override
    public void fill(Order order) {
        OrderItemExample example = new OrderItemExample();
        example.or().andOrder_idEqualTo(order.getId());
        example.setOrderByClause("id desc");
        List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
        setProduct(orderItems);

        float total = 0;
        int totalNumber = 0;
        for (OrderItem oi : orderItems) {
            total += oi.getNumber() * oi.getProduct().getPrice();
            totalNumber += oi.getNumber();
        }
      //  order.setOrderItems(orderItems);
    }
    public void setProduct(List<OrderItem> ois) {
        for (OrderItem oi : ois) {
            setProduct(oi);
        }
    }

    private void setProduct(OrderItem oi) {
        Product p = productService.get(oi.getProductId());
        oi.setProduct(p);
    }
}
